/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Text;
using System.Web;
using System.Web.UI;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;

using DotNetNuke.Modules.Articles.Common;
using ArticleConfig = DotNetNuke.Modules.Articles.Common.ArticleSettings;
using Constants = DotNetNuke.Modules.Articles.Common.Constants;
using DotNetNuke.Modules.Articles.Framework;
using DotNetNuke.ModuleToolkit.Modules.Skinning.SkinObjects;
using DotNetNuke.ModuleToolkit.Common;

#endregion

namespace DotNetNuke.Modules.Articles.UI
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ArticleSkinObjectBase class is a Base Class for all the Skin Objects in the
    /// project
    /// </summary>
    /// <history>
    /// 	[cnurse]	02/01/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)
    ]
    public class ArticleSkinObjectBase : 
        ModuleSkinObjectBase
    {

        #region Private Members

        private int _ArticleIndex = Null.NullInteger;

        #endregion

        #region Protected Members

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the ArticleId of the current Article.
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/01/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected int ArticleId
        {
            get
            {
                int _ArticleId = Null.NullInteger;
                if (ParentArticlesModule != null)
                    _ArticleId = ParentArticlesModule.ArticleId;
                return _ArticleId;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Selected Article
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/01/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected ArticleInfo Article
        {
            get
            {
                ArticleInfo _Article = null;
                if (ArticleIndex > Null.NullInteger)
                {
                    //Use ArticleIndex
                    if ((ParentArticlesModule != null) &&
                            (ParentArticlesModule.Articles != null) &&
                            (ParentArticlesModule.Articles.Count > ArticleIndex))
                        _Article = (ArticleInfo)ParentArticlesModule.Articles[ArticleIndex];
                }
                else
                {
                    //Use ArticleId
                    if (ParentArticlesModule != null)
                        _Article = ParentArticlesModule.Article;
                }

                return _Article;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the List of Articles
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/01/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected List<ArticleInfo> Articles
        {
            get
            {
                List<ArticleInfo> _Articles = new List<ArticleInfo>();
                if (ParentArticlesModule != null)
                    _Articles = ParentArticlesModule.Articles;
                return _Articles;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets whether the current user can edit the current article
        /// </summary>
        /// <history>
        /// 	[cnurse]	09/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected bool CanCreateArticle
        {
            get
            {
                bool canCreate = false;

                //Article Creators have permission to Create articles
                if (ParentArticlesModule != null && (ParentArticlesModule.HasCreatePermission))
                    canCreate = true;

                return canCreate;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets whether the current user can delete the current article
        /// </summary>
        /// <history>
        /// 	[cnurse]	10/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected bool CanDeleteArticle
        {
            get
            {
                bool canDelete = false;

                if (ParentArticlesModule != null)
                {
                    //Users with Article Delete permission can Delete articles
                    if (ParentArticlesModule.HasDeletePermission)
                        canDelete = true;

                    //Article Creators have permission to Delete their own articles (if enabled in Setting)
                    ArticleConfig config = ArticleConfig.GetSettings(ModuleId, TabModuleId);
                    bool creatorCanDelete = config.CreatorCanDelete;

                    if (creatorCanDelete && ParentArticlesModule.HasCreatePermission && (Article != null && (Article.CreatedByUser == UserId)))
                        canDelete = true;
                }

                return canDelete;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets whether the current user can edit the current article
        /// </summary>
        /// <history>
        /// 	[cnurse]	09/06/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected bool CanEditArticle
        {
            get
            {
                bool canEdit = false;

                if (ParentArticlesModule != null)
                {
                    //Article Moderators have permission to Edit articles that have been submitted
                    if (ParentArticlesModule.HasModeratePermission && (Article != null && Article.Status != PublishedStatus.Draft))
                        canEdit = true;

                    //Article Creators have permission to Edit their own articles
                    if (ParentArticlesModule.HasCreatePermission && (Article != null && (Article.CreatedByUser == UserId)))
                        canEdit = true;
                }
                return canEdit;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets whether the current user can edit the current section
        /// </summary>
        /// <history>
        /// 	[cnurse]	09/08/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected bool CanEditSection
        {
            get { return (ParentArticlesModule != null && ParentArticlesModule.IsEditable); }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the page No of the current Page
        /// </summary>
        /// <history>
        /// 	[cnurse]	12/01/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected MultiPageMode PageMode
        {
            get
            {
                MultiPageMode mode = MultiPageMode.MultiPage;
                if (HttpContext.Current.Request.QueryString[Constants.PARAM_PageMode] != null)
                {
                    mode = (MultiPageMode)Enum.Parse(typeof(MultiPageMode), HttpContext.Current.Request.QueryString[Constants.PARAM_PageMode]);
                }
                return mode;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Articles Module Base for this control
        /// </summary>
        /// <history>
        ///     [cnurse]  05/20/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected ArticlesModuleBase ParentArticlesModule
        {
            get { return (ArticlesModuleBase)base.ParentModule; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the SectionId of the current Section.
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/01/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected int SectionId
        {
            get
            {
                int _SectionId = Null.NullInteger;
                if (ParentArticlesModule != null)
                    _SectionId = ParentArticlesModule.SectionId;
                return _SectionId;
            }
            set
            {
                if (ParentArticlesModule != null)
                    ParentArticlesModule.SectionId = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Section to display
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/01/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected SectionInfo Section
        {
            get
            {
                SectionInfo _Section = new SectionInfo();
                if (ParentArticlesModule != null)
                    _Section = ParentArticlesModule.Section;
                return _Section;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The TabId of the Detail Page for the Article
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/01/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected int TabId
        {
            get
            {
                int _TabId = Null.NullInteger;
                if (ParentArticlesModule != null)
                    _TabId = ParentArticlesModule.ViewTabId;
                return _TabId;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The UserId of the User 
        /// </summary>
        /// <history>
        /// 	[cnurse]	09/07/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected int UserId
        {
            get
            {
                int _UserId = Null.NullInteger;
                if (ParentArticlesModule != null)
                    _UserId = ParentArticlesModule.UserId;
                return _UserId;
            }
        }

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Index of the Article in the Articles collection to display
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/13/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(true),
        DefaultValue(-1),
        Description("Set the Index of the Article.")]
        public int ArticleIndex
        {
            get { return _ArticleIndex; }
            set { _ArticleIndex = value; }
        }

        #endregion

    }
}
